<?php

require_once( 'recaptcha/recaptchalib.php' );

class RecaptchaException extends Exception { }


class Recaptcha {

static protected $error = null;

static public function checkAnswer ($challenge, $response, $extra_params = array())
{
	$privkey = config::__getMe( ) ->recaptcha__privatekey;
	$remoteip = request::__getMe( ) ->getIp( );
	fb::log( $privkey, 'cool' );
	if( !$privkey ) throw new RecaptchaException( 'no-private-key' );
	
	$r = recaptcha_check_answer( $privkey, $remoteip, $challenge, $response, $extra_params );
	self::$error = $r ->error;
	
	return $r;
}

static public function getHtml ($error = null, $use_ssl = false)
{
	$pubkey = config::__getMe( ) ->recaptcha__publickey;
	if( !$pubkey ) throw new RecaptchaException( 'no-public-key'.$pubkey );
	
	$error = !is_null( $error ) ? $error : self::$error;
	
	return recaptcha_get_html ($pubkey, $error, $use_ssl);
}

}

